<?php

/**
 * 库存调整管理
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	库存调整
 * @package  	Model
 * @author    	黄明兴
 * @version 	2.1,2012-07-22
*/

class AdjustPublicModel extends RelationCommonModel {
	/// 定义真实表名
	protected $tableName = 'adjust';
	/// 定义索引字段
	public $id;
	/// 定义表关联
	protected $_link	 = array('detail' => 
									array(
										'mapping_type'	=> HAS_MANY,
										'foreign_key' 	=> 'adjust_id',
										'class_name'	=> 'adjust_detail',
									)
								);	
	/// 自动验证设置
	protected $_validate	 =	 array(
			array("adjust_date",'require',"require",1), //1为必须验证
			array("currency_id",'require',"require",1), //1为必须验证
			array("",'_validDetail','require',1,'validDetail'),
			array('','validQuantity','',1,'callbacks'), 
		);
	/// 验证表单明细
	protected $_validDetail	 =	 array(
			array("product_id",'require','require',1),
			array("warehouse_id",'require','require',1),
			array("color_id",'require','require',0),
			array("size_id",'require','require',0),
			array("quantity",'double','double',1),
			array("capability",'z_integer','z_integer',0),
			array("dozen",'z_integer','z_integer',0),
			array("price",'double','double',1),
		);
		
	/// 自动填充
	protected $_auto = array(
							array("create_time ", "date", 1, "function", "Y-m-d H:i:s"), // 创建时间	
							array("update_time ", "date", 2, "function", "Y-m-d H:i:s"), // 更新时间					
						);
							
	/**
	 * 查看调整明细
	 *
	 * @return  array
	 */
	public function view(){ 
		return $this->getInfo($this->id);
	}
	
	/**
	 * 编辑调整明细
	 *
	 * @return  array
	 */
	public function edit(){
		return $this->getInfo($this->id);
	}
	
	/**
	 * 获取明细信息(用于查看/编辑)
	 *
	 * @param int $id
	 * @return array
	 */
	public  function getInfo($id) {		
		$rs = $this->find($id);
		$rs['detail'] = M('AdjustDetail')->field('*,(quantity*capability*dozen) AS sum_quantity,(quantity*capability*dozen*price) AS money')->where('adjust_id='.$id)->select();
		return _formatListRelation($rs);
	}
	 
	/**
	 * 所有订单列表SQL
	 *
	 * @return  array
	 */
	function indexSql(){ 
		
		$count 	= $this->exists('select 1 from adjust_detail where adjust_id=adjust.id and '.getWhere($_POST['detail']))->where(getWhere($_POST['main']))->count();
		$this->setPage($count);
		$ids	= $this->field('id')->exists('select 1 from adjust_detail where adjust_id=adjust.id and '.getWhere($_POST['detail']))->where(getWhere($_POST['main']))->order('adjust_no desc')->page()->selectIds();
		$info['from'] 	= 'adjust a LEFT JOIN adjust_detail b ON a.id = b.adjust_id';
		$info['group'] 	= ' group by a.id order by adjust_no desc';
		$info['where'] 	= ' where a.id in'.$ids;
		$info['field'] 	= 'a.id AS id,a.adjust_no AS adjust_no,a.adjust_date AS adjust_date,a.currency_id AS currency_id,b.adjust_id,b.product_id AS product_id,b.size_id AS size_id,b.quantity AS quantity,b.capability AS capability,b.warehouse_id AS warehouse_id,sum(quantity*capability*dozen) AS sum_quantity,sum(quantity) AS total_quantity,sum(quantity*capability*dozen*price) AS money';
		return  'select '.$info['field'].' from '.$info['from'].$info['where'].$info['group'];
	}
	
}